class Solution
{
public:
    string frequencySort(string s)
    {
        unordered_map<char, int> freqMap;
        for (char c : s)
            freqMap[c]++;

        int maxFreq = 0;
        for (const auto& [_, freq] : freqMap)
            maxFreq = max(maxFreq, freq);

        vector<vector<char>> buckets(maxFreq + 1);
        for (const auto& [element, freq] : freqMap)
            buckets[freq].push_back(element);

        string ans;
        for (int i = maxFreq; i >= 0; --i)
            if (!buckets[i].empty())
                for (char c : buckets[i])
                    for (int j = 0; j < i; ++j)
                        ans += c;

        return ans;
    }
};